<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <jsp:include page="../inc.jsp"></jsp:include>
</head>
<body>
<table id="dg" style="width: 100%;height: 80%"></table>
<script type="text/javascript">
    var dataGrid;
    var data_from;
    $(function () {
        dataGrid = $('#dg').datagrid({
            url: "${pageContext.request.contextPath}/label/all",
            singleSelect: true,
            columns: [[
                {field: 'id', title: 'id', width: "50%"},
                {field: 'name', title: '名称', width: "50%"},
            ]],
            toolbar: [{
                iconCls: 'icon-edit',
                handler: function () {
                    var row = dataGrid.datagrid('getSelected');
                    if (row) {
                        data_from = parent.$.modalDialog({
                            title: '添加修改',
                            width: 500,
                            height: 300,
                            href: '${pageContext.request.contextPath}/label/update/' + row.id,
                            buttons: [{
                                text: '修改',
                                iconCls: 'icon-ok',
                                handler: function () {
                                    parent.$.modalDialog.openner_dataGrid = dataGrid;
                                    parent.window.submit_label_from();
                                }
                            }]
                        });
                    } else {
                        parent.$.messager.alert('提示', '请勾选要修改的记录！');
                    }

                }
            }, '-', {
                iconCls: 'icon-add',
                handler: function () {
                    data_from = parent.$.modalDialog({
                        title: '添加标签',
                        width: 500,
                        height: 300,
                        href: '${pageContext.request.contextPath}/label/add',
                        buttons: [{
                            text: '添加',
                            iconCls: 'icon-ok',
                            handler: function () {
                                parent.$.modalDialog.openner_dataGrid = dataGrid;
                                parent.window.submit_label_from();
                            }
                        }]
                    });
                }
            }, '-', {
                iconCls: 'icon-cancel',
                handler: function () {
                    var row = dataGrid.datagrid('getSelected');
                    if (row) {
                        $.ajax({
                            type: "DELETE",
                            url: "${pageContext.request.contextPath}/label/delete/" + row.id,
                            success: function (data) {
                                dataGrid.datagrid('reload');
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.info(XMLHttpRequest);
                                console.info(textStatus);
                                console.info(errorThrown);
                            }
                        });
                    } else {
                        parent.$.messager.alert('提示', '请勾选要删除的记录！');
                    }
                }
            }],
            loadFilter: function (data) {
                data = data.data;
                console.info({"total": data.length, "rows": data});
                return {"total": data.length, "rows": data};
            }
        });
    });
</script>
</body>
</html>
